$(function() {
    /**
      查询所有工序
    **/
    function getProcess() {
        $.ajax({
                // url: '../json/getProcess.json',
                url: 'http://localhost:8080/getProcess',
                type: 'get',
                // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                // data: { method: 'getProcess' },
            })
            .done(function(data) {
                console.log("getProcess");
                var data = data.data;
                var html = "";
                html += ` <select data-placeholder="工序选择" class="chosen-select" multiple tabindex="6">`
                for (var i = 0; i < data.length; i++) {
                    html += `<option>${data[i]}</option>`;
                }
                html += `</select>`;
                // console.log(html)
                $(html).appendTo('.work p:first-child');
            }).done(function() {
                $(".chosen-select").chosen();
            })
            .fail(function() {
                console.log("error");
            })
    }
    getProcess();



    /**
      查询生产质量报表
    **/
    function getProducts(processName, startTime, endTime, batch,productlineNum) {
        $.ajax({
                // url: '../json/getProducts.json',
                url: 'http://localhost:8080/getProducts',
                type: 'get',
                // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                data: {
                    processName: processName,
                    startTime: startTime,
                    endTime: endTime,
                    batch: batch,
                    productlineNum:productlineNum
                },
            })
            .done(function(data) {
                console.log("getProducts");
                var data = data.data;
                var html = "";

                for (var i = 0; i < data.length; i++) {
                    html += ` <tr>
                                <td rowspan="${data[i].children.length}">${data[i].processName}</td>
                                <td>${data[i].children[0].batch}</td>
                                <td>${data[i].children[0].productionTime}</td>
                                <td>${data[i].children[0].productCount}</td>
                                <td>${data[i].children[0].productBadCount}</td>
                            </tr>`;
                    for (var j = 1; j < data[i].children.length; j++) {
                        html += `<tr>
                               <td>${data[i].children[j].batch}</td>
                                <td>${data[i].children[j].productionTime}</td>
                                <td>${data[i].children[j].productCount}</td>
                                <td>${data[i].children[j].productBadCount}</td>
                            </tr>`;
                    }

                }
                // console.log(html)
                $(html).appendTo('.pro_head table');
            }).done(function() {
                $(".chosen-select").chosen();
            })
            .fail(function() {
                console.log("error");
            })
    }




    $("body").on('click', '.search1', function(event) {
        event.preventDefault();
        /* Act on the event */
        var processName;
        if ($('.chosen-select').val() == null) {
            processName = "";
        } else {
            processName = $('.chosen-select').val().toString();
        }
        var startTime=$('#start').val();
        var endTime=$('#end').val();
        var batch=$('.batch').val();
        var productlineNum=Number($('.productlineNum').val());
        getProducts(processName,startTime,endTime, batch,productlineNum);

    });

    /**
      根据员工号查询员工报表
    **/
    function getEmpInfos(employeeCode,startTime,endTime) {
        $.ajax({
                // url: '../json/getEmpInfos.json',
                url: 'http://localhost:8080/getEmpInfos',
                type: 'get',
                // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                data: {
                    employeeCode: employeeCode,
                    startTime: startTime,
                    endTime: endTime
                },
            })
            .done(function(data) {
                console.log("getEmpInfos");
                var data = data.data;
                // console.log(data)
                $(".employeeCode").html(data.employeeCode);
                var html1 = "";
                var html2 = "";
                var process=data.employWorkResponse;
                for (var i = 0; i < process.length; i++) {
                    html1 += `<th>${process[i].processName}</th>`;
                     html2 += `<td>${process[i].productCount}</td>`;
                }
                // console.log(html)
                $(html1).appendTo('.process');
                $(html2).appendTo('.count');
            }).done(function() {
                $(".chosen-select").chosen();
            })
            .fail(function() {
                console.log("error");
            })
    }

    $("body").on('click', '.search2', function(event) {
        event.preventDefault();
        /* Act on the event */
        var employeeCode=$(".employeeCode").val();
        var startTime=$('#start').val();
        var endTime=$('#end').val();
        getEmpInfos(employeeCode, startTime, endTime);

    });
    // getEmpInfos();
})